package cn.edu.scau.cmi.chenyiqiu.composite.safe;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import cn.edu.scau.cmi.chenyiqiu.composite.consistent.ConsistentOrganizationComponent;
import cn.edu.scau.cmi.chenyiqiu.composite.consistent.ConsistentOrganizationComposite;
import cn.edu.scau.cmi.chenyiqiu.composite.consistent.ConsistentPerson;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;

//BUG一堆，而且很难用
public class controller {
	@FXML
	private TextField index1;
	@FXML
	private TextField Comname;
	@FXML
	private TextField index2;
	@FXML
	private TextField nodename;
	@FXML
	private TextField delname;
	@FXML
	private TreeItem<String> rootItem;
	@FXML
	private TreeView<String> treeView = new TreeView<>();
	
	private Set<Set<ConsistentOrganizationComponent>> set1 = new HashSet<Set<ConsistentOrganizationComponent>>();
	private Set<Set<SafeComponent>> set1_1 = new HashSet<Set<SafeComponent>>();
	private int mode = 1;
	ConsistentOrganizationComponent all = new ConsistentOrganizationComposite();
	SafeComposite all1 = new SafeComposite();
	public void Safe(){
		rootItem = new TreeItem<> ("各大组织");
		treeView.setRoot(rootItem);
		mode = 2;
		all.setName("各大组织");
		rootItem.setExpanded(true);
		treeView.setEditable(true);
		treeView.setShowRoot(false);
	}
	
	public void Consistent(){
		rootItem = new TreeItem<> ("各大组织");
		treeView.setRoot(rootItem);
		mode = 1;
		all.setName("各大组织");
		rootItem.setExpanded(true);
		treeView.setEditable(true);
		treeView.setShowRoot(false);
	}
	
	public void createPerson(){
		int tu = 0;
		if(mode == 1) {
			ConsistentOrganizationComponent p = new ConsistentPerson();
			p.setName(nodename.getText());
			ConsistentOrganizationComponent parent;
			String OrgaNa = index2.getText();
			Set<ConsistentOrganizationComponent> set = all.getChildrens();
			set1.add(set);
			while(set1.size() > 0) {
				Iterator iteSet1 = set1.iterator();
				Set<ConsistentOrganizationComponent> set2 = (Set<ConsistentOrganizationComponent>) iteSet1.next();
				Iterator iteSet2 = set2.iterator();
				while(iteSet2.hasNext()) {
					ConsistentOrganizationComponent p1 = (ConsistentOrganizationComponent) iteSet2.next();
					if(p1 instanceof ConsistentOrganizationComposite) {
						if(p1.getName().equals(OrgaNa)) {
							tu = 1;
							parent = p1;
							parent.addChildren(p);
						}else {
							set1.add(p1.getChildrens());
						}	
					}
				}
				set1.remove(set2);
				if(tu == 1) {
					break;
				}
				if(tu == 1) {
					break;
				}
			}
			if(tu == 0) {
				parent = new ConsistentOrganizationComposite();
				parent.setName(OrgaNa);
				parent.addChildren(p);
				all.addChildren(parent);
				TreeItem<String> item = new TreeItem<> (parent.getName());
				TreeItem<String> item1 = new TreeItem<> (p.getName());
				item.setExpanded(true);
				item.getChildren().add(item1);
				rootItem.getChildren().add(item);
			}else {
				ObservableList<TreeItem<String>> ob = rootItem.getChildren();
				addI(ob, p, OrgaNa);
			}
		}
		else {
			SafeComponent p = new SafeLeaf();
			p.setName(nodename.getText());
			SafeComposite parent;
			String OrgaNa = index2.getText();
			Set<SafeComponent> set = all1.getChildrens();
			set1_1.add(set);
			while(set1_1.size() > 0) {
				Iterator iteSet1 = set1_1.iterator();
				Set<SafeComponent> set2 = (Set<SafeComponent>) iteSet1.next();
				Iterator iteSet2 = set2.iterator();
				while(iteSet2.hasNext()) {
					SafeComponent p1 = (SafeComponent)iteSet2.next();
					if(p1 instanceof SafeComposite) {
						if(p1.getName().equals(OrgaNa)) {
							tu = 1;
							parent = (SafeComposite)p1;
							parent.addChildren(p);
						}else {
							set1_1.add(p1.getChildrens());
						}	
					}
					if(tu == 1) {
						break;
					}
				}
				set1_1.remove(set2);
				if(tu == 1) {
					break;
				}
			}
			if(tu == 0) {
				parent = new SafeComposite();
				parent.setName(OrgaNa);
				parent.addChildren(p);
				all1.addChildren((SafeComponent)parent);
				TreeItem<String> item = new TreeItem<> (parent.getName());
				TreeItem<String> item1 = new TreeItem<> (p.getName());
				item.setExpanded(true);
				item.getChildren().add(item1);
				rootItem.getChildren().add(item);
			}else {
				ObservableList<TreeItem<String>> ob = rootItem.getChildren();
				addI(ob, p, OrgaNa);
			}
		}
		set1.clear();
		set1_1.clear();
		nodename.clear();
		index2.clear();
	}
	
	public void createOrga() {
		int tu = 0;
		if(mode == 1) {
			ConsistentOrganizationComponent p = new ConsistentOrganizationComposite();
			p.setName(Comname.getText());
			ConsistentOrganizationComponent parent;
			String OrgaNa = index1.getText();
			Set<ConsistentOrganizationComponent> set = all.getChildrens();
			set1.add(set);
			while(set1.size() > 0) {
				Iterator iteSet1 = set1.iterator();
				Set<ConsistentOrganizationComponent> set2 = (Set<ConsistentOrganizationComponent>) iteSet1.next();
				Iterator iteSet2 = set2.iterator();
				while(iteSet2.hasNext()) {
					ConsistentOrganizationComponent p1 = (ConsistentOrganizationComponent)iteSet2.next();
					if(p1 instanceof ConsistentOrganizationComposite) {
						if(p1.getName().equals(OrgaNa)) {
							tu = 1;
							parent = p1;
							parent.addChildren(p);
							break;
						}else {
							set1.add(p1.getChildrens());
						}	
					}
					if(tu == 1) {
						break;
					}
				}
				set1.remove(set2);
				if(tu == 1) {
					break;
				}
			}
			if(tu == 0) {
				parent = new ConsistentOrganizationComposite();
				parent.setName(OrgaNa);
				parent.addChildren(p);
				all.addChildren(parent);
				TreeItem<String> item = new TreeItem<> (parent.getName());
				TreeItem<String> item1 = new TreeItem<> (p.getName());
				item.setExpanded(true);
				item1.setExpanded(true);
				item.getChildren().add(item1);
				rootItem.getChildren().add(item);
			}else {
				ObservableList<TreeItem<String>> ob = rootItem.getChildren();
				addI(ob, p, OrgaNa);
			}
		}else {
			SafeComponent p = new SafeComposite();
			p.setName(Comname.getText());
			SafeComposite parent;
			String OrgaNa = index1.getText();
			Set<SafeComponent> set = all1.getChildrens();
			set1_1.add(set);
			while(set1_1.size() > 0) {
				Iterator iteSet1 = set1_1.iterator();
				Set<SafeComponent> set2 = (Set<SafeComponent>) iteSet1.next();
				Iterator iteSet2 = set2.iterator();
				while(iteSet2.hasNext()) {
					SafeComponent p1 = (SafeComponent)iteSet2.next();
					if(p1 instanceof SafeComposite) {
						if(p1.getName().equals(OrgaNa)) {
							tu = 1;
							parent = (SafeComposite)p1;
							parent.addChildren(p);
							break;
						}else {
							set1_1.add(p1.getChildrens());
						}	
					}
				}
				set1_1.remove(set2);
				if(tu == 1) {
					break;
				}
			}
			if(tu == 0) {
				parent = new SafeComposite();
				parent.setName(OrgaNa);
				parent.addChildren(p);
				all1.addChildren((SafeComponent)parent);
				TreeItem<String> item = new TreeItem<> (parent.getName());
				TreeItem<String> item1 = new TreeItem<> (p.getName());
				item.setExpanded(true);
				item1.setExpanded(true);
				item.getChildren().add(item1);
				rootItem.getChildren().add(item);
			}else {
				ObservableList<TreeItem<String>> ob = rootItem.getChildren();
				addI(ob, p, OrgaNa);
			}
		}
		set1.clear();
		set1_1.clear();
		Comname.clear();
		index1.clear();
	}
	
	public void addI(ObservableList<TreeItem<String>> ob, ConsistentOrganizationComponent p,
			String OrgaNa) {
		for(int i =0; i < ob.size(); i++) {
			TreeItem<String> item = ob.get(i);
			if(item.isExpanded()) {
				if(item.getValue().equals(OrgaNa)) {
					TreeItem<String> item1 = new TreeItem<String>(p.getName());
					item1.setExpanded(true);
					item.getChildren().add(item1);
					break;
				}else {
					addI(item.getChildren(), p, OrgaNa);
				}
			}
		}
	}
	
	public void addI(ObservableList<TreeItem<String>> ob, SafeComponent p,
			String OrgaNa) {
		for(int i =0; i < ob.size(); i++) {
			TreeItem<String> item = ob.get(i);
			if(item.isExpanded()) {
				if(item.getValue().equals(OrgaNa)) {
					TreeItem<String> item1 = new TreeItem<String>(p.getName());
					item.setExpanded(true);
					item1.setExpanded(true);
					System.out.println(p.getName() + item1.isExpanded());
					item.getChildren().add(item1);
					break;
				}else {
					addI(item.getChildren(), p, OrgaNa);
				}
			}
		}
	}
	
	public void delete() {
		String name=delname.getText();
		SafeComponent p=new SafeComposite();
		
		
	}
	
	
}
